package room;


import java.util.Collection;
import java.util.Iterator;
import javax.swing.DefaultListModel;

import network.enums.ServerPackets;
import gui.GUIManager;

import app.AppGlobals;


public class RoomHandler {
	
	public static Boolean updateGameList = true;
	
	public static void addRoomEntry(Room room){
		if(AppGlobals.roomList != null){
			if(!AppGlobals.roomList.contains(room)){
				AppGlobals.roomList.put(room.roomName, room);
				updateRoomListbox();
			}
		}	
	}
	
	public static void addRoomArray(Room[] rooms){
		AppGlobals.roomList.clear();
		if(AppGlobals.roomList != null){
			for (int i = 0; i < rooms.length; i++)
			    addRoomEntry(rooms[i]);
		}
	}
	
	public static void removeRoomEntry(String roomname){
		if(AppGlobals.roomList != null){
			if(AppGlobals.roomList.containsKey(roomname)){
				Room r = AppGlobals.roomList.get(roomname);
				RoomListModel listModel = AppGlobals.unrankedListModel;
				if(r.isRanked)
					listModel = AppGlobals.rankedListModel;
				
				for(int i = 0; i < listModel.size();i++){
					Room s = listModel.get(i);
					if(s.roomName.equals(roomname)){
						listModel.removeElement(s);
					}
				}
				AppGlobals.roomList.remove(roomname);
				
				//updateRoomListbox();
			}
		}	
	}
	
	public static void onRoomStart(String roomname){
		if(AppGlobals.roomList != null){
			if(AppGlobals.roomList.containsKey(roomname))
				((Room)AppGlobals.roomList.get(roomname)).hasStarted = true;
			
			updateRoomListbox();
		}
	}
	
	public static void updateRoom(Room room){
		if(AppGlobals.roomList != null){
			if(AppGlobals.roomList.containsKey(room.roomName)){
				AppGlobals.roomList.put(room.roomName, room);	
			}else{
				addRoomEntry(room);
			}
			updateRoomListbox();
		}
	}
	
	public static int getRoomEntryPosInListbox(Room room){
		DefaultListModel<Room> listModel = AppGlobals.unrankedListModel;
		if(room.isRanked){
			listModel = AppGlobals.rankedListModel;
		}
		System.out.println("listModel Size: " + listModel.getSize());
		for(int i = 0; i < listModel.getSize(); i++){
			Room s = listModel.get(i);
			if(s.roomName.equals(room.roomName))
				return i;
		}
		
		return -1;
	}
	
	public synchronized static void updateRoomListbox(){
		if(AppGlobals.unrankedListModel == null 
				|| AppGlobals.rankedListModel == null 
				|| AppGlobals.roomList == null) return;

		for (Iterator<String> iter = AppGlobals.roomList.keySet().iterator(); iter.hasNext();) {
			Room r = AppGlobals.roomList.get(iter.next());
			//String next = r.roomName;
			RoomListModel listModel = AppGlobals.rankedListModel;
			
			if(!r.isRanked)
				listModel = AppGlobals.unrankedListModel;
			
			Room lRoom = listModel.getRoomByName(r.roomName);
			if(lRoom == null){
				if(!r.hasStarted || !GUIManager.serverTabFilterCheckbox.isSelected())
					listModel.addElement(r);
			}else{
				if(r.hasStarted){
					if(GUIManager.serverTabFilterCheckbox.isSelected()){
						listModel.removeElement(lRoom);
					}	
				}else{
					listModel.setElementAt(r, listModel.getRoomPosition(lRoom));
				}
			}		
        }

	}
	
	public static void sendGameListRequest(){
		AppGlobals.gameServer.sendPacket(ServerPackets.GameList);
	
	}
	
	public static void updateServerDetails(){
		if(AppGlobals.roomList == null) return;
		int[] statsCount = getStatsCount(); // [0] = is ranked game, [1] = hasStarted, [2] = player Count
		int totalRooms = AppGlobals.roomList.size();
		GUIManager.serverTabPlayersLabel.setText(Integer.toString(statsCount[2]));;
		GUIManager.serverTabRankedLabel.setText(Integer.toString(statsCount[0]));
		GUIManager.serverTabRoomsLabel.setText(Integer.toString(totalRooms));
		GUIManager.serverTabUnrankedLabel.setText(Integer.toString(totalRooms - statsCount[0]));
		GUIManager.serverTabOpenRoomsLabel.setText(Integer.toString( statsCount[1]));
	}
	
	public static int[] getStatsCount(){
		int[] count = {0,0,0};
		if(AppGlobals.roomList == null || AppGlobals.roomList.isEmpty()) return count;
		
		
		
		Collection<Room> rooms = AppGlobals.roomList.values();
		Iterator<Room> it = rooms.iterator();
		
		while(it.hasNext()){
			Room room = (Room) it.next();
			if(room != null){
				if(room.isRanked == true)
					count[0]++;
				
				if(!room.hasStarted)
					count[1]++;
				
				if(room.playerList != null)
					count[2] = count[2] + room.playerList.length;
			}
		}
		return count;	
	}

}
